package com.lepu.app.fun.index.fragment;

import java.util.ArrayList;

import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragment;
import com.eyzhs.app.R;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class FragmentIndexSearch extends BaseFragment {

	private View mMainView = null;
	private boolean mInitDataSuccess = false;// 是否加载完数据

	private ViewPager mViewPager = null;
	private RelativeLayout mTabLayoutOne = null;
	private RelativeLayout mTabLayoutTwo = null;

	private View mTopLine1;
	private View mTopLine2;
	private int mCurrentTab = 0;

	private String mKey = "";

	FragmentIndexSearchArticle mFragmentArticle;
	FragmentIndexSearchTopic mFragmentTopic;

	@SuppressLint("InflateParams")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mMainView = LayoutInflater.from(getActivity()).inflate(
				R.layout.index_search_fragment, null, false);
		init();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		ViewGroup p = (ViewGroup) mMainView.getParent();
		if (p != null) {
			p.removeAllViewsInLayout();
		}
		return mMainView;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		mInitDataSuccess = false;
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser) {
			if (!mInitDataSuccess) {
				// 第一次进入页面获取数据
				mInitDataSuccess = true;
			}
		}
	}

	/*
	 * 初始化ui控件，不要初始化数据！！！！
	 * 
	 * 初始化控件，需要用mMainView.findViewById 不能使用findViewById，否则程序会闪退的
	 */
	private void init() {

		mKey = getArguments().getString("key");
		// tab
		mTabLayoutOne = (RelativeLayout) mMainView
				.findViewById(R.id.tabLayout1);
		mTabLayoutTwo = (RelativeLayout) mMainView
				.findViewById(R.id.tabLayout2);

		mTabLayoutOne.setOnClickListener(mViewClickListener);
		mTabLayoutTwo.setOnClickListener(mViewClickListener);

		mTopLine1 = (View) mMainView.findViewById(R.id.tabLine1);
		mTopLine2 = (View) mMainView.findViewById(R.id.tabLine2);

		// content
		ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
		mFragmentArticle = new FragmentIndexSearchArticle();
		mFragmentTopic = new FragmentIndexSearchTopic();

		Bundle dataArticle = new Bundle();
		dataArticle.putString("key", mKey);
		mFragmentArticle.setArguments(dataArticle);
		fragmentList.add(mFragmentArticle);

		Bundle dataTopic = new Bundle();
		dataTopic.putString("key", mKey);
		mFragmentTopic.setArguments(dataTopic);
		fragmentList.add(mFragmentTopic);

		mViewPager = (ViewPager) mMainView.findViewById(R.id.myViewPager);
		mViewPager.setAdapter(new FragmentPageAdapter(getActivity()
				.getSupportFragmentManager(), fragmentList, null, null));
		mViewPager.setOnPageChangeListener(new MyViewChangeListener());

		mViewPager.setCurrentItem(0);
		updateTabLine(0);
	}

	public void request(boolean isMore,int currentTab) {
		// new add
		mCurrentTab = currentTab;
		mViewPager.setCurrentItem(currentTab);
		updateTabLine(currentTab);
		
		if (mCurrentTab == 0) {
			mFragmentArticle.request(false);
		} else {
			mFragmentTopic.request(false);
		}
	}
	
	public void clear()
	{
		mFragmentArticle.clear();
		mFragmentTopic.clear();
	}

	View.OnClickListener mViewClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tabLayout1:
				if (mCurrentTab == 0)
					return;

				mViewPager.setCurrentItem(0);
				updateTabLine(0);
				break;
			case R.id.tabLayout2:

				if (mCurrentTab == 1)
					return;

				mViewPager.setCurrentItem(1);
				updateTabLine(1);
				break;
			default:
				break;
			}
		}
	};

	private void updateTabLine(int currentIndex) {
		if (currentIndex == 0) {
			mTabLayoutOne.setSelected(true);
			mTabLayoutTwo.setSelected(false);
			mTopLine1.setVisibility(View.VISIBLE);
			mTopLine2.setVisibility(View.INVISIBLE);
		} else {
			mTabLayoutOne.setSelected(false);
			mTabLayoutTwo.setSelected(true);
			mTopLine1.setVisibility(View.INVISIBLE);
			mTopLine2.setVisibility(View.VISIBLE);
		}
	}

	class MyViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {

		}

		@Override
		public void onPageSelected(int index) {
			mCurrentTab = index;
			mViewPager.setCurrentItem(index);
			updateTabLine(index);
		}
	}
}
